package com.idione.util;

import android.content.Context;
import android.os.SystemClock;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Chronometer;

public class MyChronometer extends Chronometer{

	private long elapsedTime = 0;
	private long currentTime = 0;
	private	Boolean resume = false;
	
	/*----------------------------------------------------------------------* 
	 * Constructor															*
	 *----------------------------------------------------------------------*/
	public MyChronometer(Context context) {
		super(context);
		log("MyChronometer(Context)");
	}
	
	public MyChronometer(Context context, AttributeSet attrs) {
		super(context, attrs);
		log("MyChronometer(Context, AttributeSet)");		
	}
	
	public MyChronometer(Context context, AttributeSet attrs, int defStyle) { 
		super(context, attrs, defStyle);
		log("MyChronometer(Context, AttributeSet, int)");
	}
	
	/*----------------------------------------------------------------------* 
	 * Methods																*
	 *----------------------------------------------------------------------*/	
	public void registerTime() {
		if (!resume) {
			currentTime = (SystemClock.elapsedRealtime()-getBase());
			this.setText(DateFormat.format("mm:ss", currentTime));
			elapsedTime = SystemClock.elapsedRealtime();
		} else {
			currentTime = elapsedTime-getBase();
			this.setText(DateFormat.format("mm:ss", currentTime));
			elapsedTime = elapsedTime + 1000;	
		}		
	}
	
	public void startClock() {
		log("startClock()");
		if (!resume) {
			this.setBase(SystemClock.elapsedRealtime());
			this.start();
		} else {
			this.start();
		}
	}
	
	public void stopClock() {
		log("stopClock()");
		
		this.stop();
		this.setText(DateFormat.format("mm:ss", currentTime));
		resume = true;
	}
	
	public void reset() {
		log("reset()");
		
		this.stop();
		this.setText("00:00");
		resume = false;
	}
	
	public long getCurrentTimeMilis() {
		log("getCurrentTimeMilis()");
		// Milliseconds
		return currentTime;
	}
		
	public long getBaseTime() {
		return this.getBase();
	}
	
	public long getElapsedTime() {
		return elapsedTime;
	}
	
	public void setBaseTime(long setBase) {
		this.setBase(setBase);
	}
	
	public void setElapsedTime(long elapsedTime) {
		this.elapsedTime = elapsedTime;
		resume = true;
	}
	
	/*------------------------------------------------------------------------------*
	 * Log																			*
	 *------------------------------------------------------------------------------*/
	private final String LOGTAG = "LogsClass " + this.getClass().getName();
	private void log(String method) {
		if (LogUtil.utilLog)
			Log.i(LOGTAG, "... method : " + this.getClass().getSimpleName() + "." + method);
	}
}
